<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Login_model extends CI_Model
  {
    private $m_database = NULL;  
    
    // Replaced the constructor for codeigniter with init func
    public function initialize($database)
    {
        $this->m_database = $database;
    }
     
    // Islogged in used for checking
    public function IsLoggedIn()
    {
        // Checking if the user is logged in
      	if($this->session->userdata('user'))
    	{
      		return true;
      	}
      	else
      	{
      		return false;
      	}
    }
    
    // Setting last visit cookie
    public function SetLastVisitCookie()
    {
        if(isset($_COOKIE['LastVisit']))
    	{
    		$_SESSION['currentLastVisit'] = $_COOKIE['LastVisit']; 		
    	}
    	
    	$month = 2592000 + time();
    	setcookie("LastVisit", date("F jS - g:i a"), $month);
    }
    
    // Retrive last visit cookie
    public function RetriveLastVisitCookie()
    {
    	$latest = "";
    	if(isset($_SESSION['currentLastVisit']))
    	{
    		$latest = $_SESSION['currentLastVisit']; 		
    	}
    	return $latest;
    }
    
    // Returns a session array with the user object 
    public function GetUser()
    {
    	return $this->session->userdata('user'); 
    }
    
    // Dologin to login
    public function DoLogin($username, $password)
    {
      $user = $this->db->get_where("users", array("username" => $username));
      
      if($user->num_rows() > 0)
      {
      	$data = $user->row();
        if($data->password == md5($password))
        {
        	$userObject = new User($data->id, $data->rank, $data->username, $data->password);
        	$this->session->set_userdata('user', $userObject);
        	
            return true;
        }
      }
      return false;
    }
    
    // Dologout used to logout & unset
    public function DoLogout()
    {
      if($this->session->userdata('user'))
      {
		$this->session->unset_userdata('user');
		$this->session->sess_destroy();
      }
    }
        
    // Automated test
    public function Test()
    {  
      $this->DoLogout();  
            
      // Checking if youre logged in
      if($this->IsLoggedIn())
      {
        echo "Error, you are logged in";
        return false;
      }
      
      // Trying to login with faulty details
      if($this->DoLogin('nouser', 'nopass'))
      {
        echo "Error you were logged in with faulty details(dologin)";
        return false;
      }
      
      // Trying to login with null
      if($this->DoLogin('', ''))
      {
        echo "Error you were logged in with null";
        return false;
      }
      
      // Loggin in with correct details
      if(!$this->DoLogin('Adamzone89', 'gZ9AB!Cabc'))
      {
        echo "Error, you where not logged in, when you should be (dologin2)";
        return false;
      }

      // Checking if you are logged in
      if(!$this->IsLoggedIn())
      {
        echo "Error, you are not logged in";  
        return false;
      }
      
      // Logging out & checking it
      $this->DoLogout();
      if($this->IsLoggedIn())
      {
        echo "Error, logout failed";
        return false;
      }

      // Attempting a combo
      if($this->DoLogin('goodUser', 'badPassword'))
      {
        echo "Error, you shouldnt be able to login with this combo";
        return false;
      }
      
      return true;
    }
    
  }
